package com.gaoding.shared.dict.controller;


import com.gaoding.framework.web.dto.PageInfo;
import com.gaoding.framework.web.util.WebUtils;
import com.gaoding.grpc.client.annotation.GdGrpcClient;
import com.gaoding.grpc.common.protobuf.Common;
import com.gaoding.grpc.common.utils.GdBeanUtils;
import com.gaoding.shared.dict.dto.request.CreateDictLanguageDTO;
import com.gaoding.shared.dict.dto.request.QueryDictLanguageDTO;
import com.gaoding.shared.dict.dto.request.UpdateDictLanguageDTO;
import com.gaoding.shared.dict.dto.response.DictLanguageRespDTO;
import com.gaoding.shared.dict.rpc.service.CreateDictLanguageRequest;
import com.gaoding.shared.dict.rpc.service.DictLanguageServiceGrpc;
import com.gaoding.shared.dict.rpc.service.QueryDictLanguageRequest;
import com.gaoding.shared.dict.rpc.service.UpdateDictLanguageRequest;
import com.google.protobuf.Int64Value;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：墨白
 * @date ：Created in 2021-02-24
 * description ：多语言信息Controller
 */
@RestController
@RequestMapping("/languages")
@Api(tags = "多语言信息接口")
@CrossOrigin
public class DictLanguageController extends BaseController<DictLanguageRespDTO> {

    @GdGrpcClient
    private DictLanguageServiceGrpc.DictLanguageServiceBlockingStub client;


    /**
     * @param query
     * @return java.util.List<com.gaoding.shared.dict.dto.response.DictLanguageRespDTO>
     * @since 2021/2/19 11:44 上午
     */

    @GetMapping("list")
    @ApiOperation("分页查询")
    public List<DictLanguageRespDTO> list(@Valid QueryDictLanguageDTO query) {
        int pageNum = Integer.max(query.getPageNum() - 1, 0);
        Common.Page page = Common.Page.newBuilder().setPageSize(query.getPageSize()).setPageNum(pageNum).buildPartial();
        QueryDictLanguageRequest.Builder builder = QueryDictLanguageRequest.newBuilder().setLanguageCode(query.getLanguageCode()!=null?query.getLanguageCode():"").setPage(page);
        if (query.getId() != null) {
            builder.setId(Int64Value.newBuilder().setValue(query.getId()).build());
        }
        val queryParam = builder.build();
        val result = client.list(queryParam)
                .getDataList().stream().map(data -> DictLanguageRespDTO.builder().languageCode(data.getLanguageCode()).languageName(data.getLanguageName()).id(data.getId()).build()).collect(Collectors.toList());
        WebUtils.buildPageInfo(PageInfo.builder().num(query.getPageNum()).size(query.getPageSize()).build());
        return result;
    }

    @PostMapping("")
    @ApiOperation("新增单个语言")
    public Long create(@Valid @RequestBody CreateDictLanguageDTO createDictLanguageDTO) {
        CreateDictLanguageRequest.Builder builder = CreateDictLanguageRequest.newBuilder();
        GdBeanUtils.copyProperties(createDictLanguageDTO, builder);
        Int64Value int64Value = client.create(builder.build());
        return int64Value.getValue();

    }

    @PutMapping("/{id}")
    @ApiOperation("更新单个语言")
    public void update(@PathVariable("id") Long id, @RequestBody @Valid UpdateDictLanguageDTO updateDictLanguageDTO) {
        updateDictLanguageDTO.setId(id);
        UpdateDictLanguageRequest.Builder builder = UpdateDictLanguageRequest.newBuilder();
        GdBeanUtils.copyProperties(updateDictLanguageDTO, builder);
        client.update(builder.build());
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除单个语言")
    public void delete(@PathVariable("id") Long id) {
        client.delete(Int64Value.of(id));
    }


    @Override
    protected String[] args(DictLanguageRespDTO dictLanguage) {
        return new String[]{dictLanguage.getLanguageCode(),dictLanguage.getLanguageCode(),dictLanguage.getLanguageName()};
    }
}
